<?php

namespace App\Controller;


class BasisDatasController extends AppController
{

  public function initialize()
  {
    parent::initialize();
  }

  /*
 * create by phpstorm
 * author：Ming
 * date：2020.11.18
 * ps：获取分类
 * */
  public function getCompanyType()
  {
    $this->loadModel('Categories');
    $data = $this->Categories->find()
      ->select(['id', 'alias', 'parent_id', 'enabled'])
      ->where(['parent_id' => 20])
      ->order(['id asc'])
      ->toArray();
    $this->G->success('Success', $data);
  }

  /*
 * create by phpstorm
 * author：Ming
 * date：2020.09.11
 * ps：通过国家id获取城市列表
 * */
  public function getZones()
  {
    if (!$this->request->is("post")) {
      $this->G->error("post_error");

      return null;
    }
    $countryId = $this->request->getData("countryId");
    $this->loadModel('Zones');
    $zones = $this->Zones->find()
      ->select(['id', 'name'])
      ->where(['country_id' => $countryId])
      ->toArray();
    if (empty($zones)) {
      $this->G->success("Success", ['list' => []]);

      return null;
    }
    $re = [];
    foreach ($zones as $k => $v) {
      $re['list'][] = [
        'label' => $v['name'],
        'value' => $v['id'],
      ];
    }
    $this->G->success("Success", $re);
  }

  /*
 * create by phpstorm
 * author：Ming
 * date：2020.09.11
 * ps：获取国家列表
 * */
  public function getCountry()
  {
    $this->loadModel('Countries');
    $countries = $this->Countries->find()
      ->select(['id', 'name'])
      ->where(['display=1'])
      ->toArray();
    if (empty($countries)) {
      $this->G->success("Success", ['list' => []]);

      return null;
    }
    $re = [];
    foreach ($countries as $k => $v) {
      $re['list'][] = [
        'label' => $v['name'],
        'value' => $v['id'],
      ];
    }
    $this->G->success("Success", $re);
  }

  /*
 * create by phpstorm
 * author：Ming
 * date：2020.09.11
 * ps：搜索商品列表
 * */
  public function search()
  {
    $this->loadModel('Products');

    $title = $this->request->getData('title');

    $mall_adblock_id = $this->request->getData('mall_adblock_id');

    $where = ['is_delete=0', 'enabled=1', 'title Like' => '%' . $title . '%'];
    if(!empty($mall_adblock_id)){
      // 根据国家馆id查找国家馆对应国家
      $this->loadModel('MallAdblocks');
      $mall_adblock = $this->MallAdblocks->findById($mall_adblock_id)->first();
      if(empty($mall_adblock)){
        $this->G->error('mall_error');

        return null;
      }
      $where['country_id'] = $mall_adblock['country_id'];
    }


    $products = [];
    if (!empty($title)) {
      $products = $this->Products->find()
        ->select(['id', 'title'])
        ->where($where)
        ->limit(50)
        ->toArray();

      if (empty($products)) {
        $this->G->success("Success", ['list' => []]);

        return null;
      }
    }
    $re = [];
    foreach ($products as $k => $v) {
      $re['list'][] = [
        'label' => $v['title'],
        'value' => $v['id'],
      ];
    }
    $this->G->success("Success", $re);
  }

  /*
 * create by phpstorm
 * author：Ming
 * date：2020.09.11
 * ps：数据字典
 * */
  public function getSysDict()
  {
    $this->loadModel('BizDicts');
    $biz_dicts = $this->BizDicts->find()
      ->select(['type', 'label', 'value'])
      ->toArray();
    if (empty($biz_dicts)) {
      $this->G->success("Success", []);

      return null;
    }
    $re = [];
    foreach ($biz_dicts as $k => $v) {
      $re[$v['type']][] = [
        'label' => $v['label'],
        'value' => $v['value'],
      ];
    }
    $this->G->success("Success", $re);
  }

}

?>
